package lzzy16;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Server extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        //用于显示内容的文本区域
        TextArea textArea = new TextArea();

//        创建一个scene 并且把它放在stage里
        Scene scene = new Scene(new ScrollPane(textArea),450,200);

        primaryStage.setTitle("Server");
        primaryStage.setScene(scene);
        primaryStage.show();

        new Thread(() -> {
            //创建服务器

            try {
                ServerSocket serverSocket = new ServerSocket(8000);
                Platform.runLater(() ->
                        textArea.appendText("Server started at "+ new Date()+ "\n")
                        );
                Socket socket = serverSocket.accept();
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

                while (true){
                    double radius = inputStream.readDouble();

                    double area = radius*radius*Math.PI;

                    outputStream.writeDouble(area);

                    Platform.runLater(() -> {
                        textArea.appendText("Radius received from client: "+radius+"\n");
                        textArea.appendText("Area is: "+area+"\n");
                    });

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
